package com.zygh.questionnairesurvey.server;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zygh.questionnairesurvey.common.CommonResult;
import com.zygh.questionnairesurvey.entity.Dept;
import com.zygh.questionnairesurvey.entity.ProblemDetails;
import com.zygh.questionnairesurvey.mapper.DeptMapper;
import com.zygh.questionnairesurvey.mapper.ProblemDetailsMapper;
import org.apache.commons.lang.StringUtils;
import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @program: questionnaire-survey
 * @description: 个人问题详情描述
 * @author: ID-Tang
 * @create: 2021-05-11 13:48
 **/
@Service
public class ProblemDetailsService {
    @Resource
    ProblemDetailsMapper problemDetailsMapper;
    @Resource
    private DeptMapper deptMapper;

    /**
     * 批量操作(如果有id则更新无id则添加)
     *
     * @param list 要添加的详情描述信息集合
     * @return 影响条数
     */
    public Integer insertAndUpdate(List<ProblemDetails> list) {

        int result = 0;
        for (ProblemDetails problemDetails : list) {
            if (null == problemDetails.getId() && StringUtils.isNotBlank(problemDetails.getDetails())) {
                problemDetails.setCreateTime(new Date());
                result += problemDetailsMapper.insert(problemDetails);
                continue;
            }

            result += problemDetailsMapper.updateById(problemDetails);
        }

        return result;
    }

    /**
     * 批量删除(可单删)
     *
     * @param ids 要删除的详情描述id集合
     * @return 影响条数
     */
    public Integer delete(List<Integer> ids) {
        return ids.stream()
                .mapToInt(id -> problemDetailsMapper.deleteById(id))
                .sum();
    }


    /**
     * 根据警号和编号查询个人问题详情列表
     *
     * @param warningsignal 警号
     * @param problemId     问题编号
     * @return
     */
    public CommonResult findDetailById(String warningsignal, String problemId) {
        List<ProblemDetails> problemDetails = problemDetailsMapper.selectList(Wrappers.<ProblemDetails>lambdaQuery()
                .eq(StringUtils.isNotBlank(warningsignal), ProblemDetails::getWarningsignal, warningsignal)
                .eq(StringUtils.isNotBlank(problemId), ProblemDetails::getProblemId, problemId));
        return new CommonResult().success(problemDetails);
    }

    /**
     * 根据部门查询该部门下个人问题详情个数
     *
     * @return
     */
    public CommonResult findDetailByDeptId() {
        List<Dept> depts = deptMapper.selectList(Wrappers.<Dept>lambdaQuery()
                .eq(Dept::getPid, 0)
                .orderByAsc(Dept::getSort));
        List<Map<String, Object>> list = new ArrayList<>();
        depts.forEach(dept -> {
            Map<String, Object> map = new HashMap<>();
            Integer detailByDeptId = problemDetailsMapper.findDetailByDeptId(dept.getDeptId());
            map.put("name", dept.getName());
            map.put("num", detailByDeptId);
            list.add(map);
        });

        return new CommonResult().success(list);
    }
}
